package com.smartfactory.sail.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.smartfactory.sail.entity.PurchaseRecord;
import com.smartfactory.sail.mapper.PurchaseRecordMapper;
import com.smartfactory.sail.service.IPurchaseRecordService;
import com.smartfactory.sail.vo.purchase.StatisticsPurchaseByCustResp;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 采购单明细 服务实现类
 * </p>
 *
 * @author 王巨壁
 * @since 2019-08-20
 */
@Service
public class PurchaseRecordServiceImpl extends ServiceImpl<PurchaseRecordMapper, PurchaseRecord> implements IPurchaseRecordService {

    private PurchaseRecordMapper purchaseRecordMapper;

    public PurchaseRecordServiceImpl(PurchaseRecordMapper purchaseRecordMapper) {
        this.purchaseRecordMapper = purchaseRecordMapper;
    }

    @Override
    public List<StatisticsPurchaseByCustResp> getStatisByCustId(Long custId) {

        QueryWrapper<PurchaseRecord> query = new QueryWrapper<>();
        query.eq("supplierId", custId);
        query.orderByAsc("updateTime");
        query.ne("status", 1);
        List<PurchaseRecord> list = this.list(query);

        HashMap<Long, StatisticsPurchaseByCustResp> map = new HashMap<>();
        for (int i = 0; i < list.size(); i++) {
            if (map.get(list.get(i).getItemId()) == null) {
                StatisticsPurchaseByCustResp res = new StatisticsPurchaseByCustResp();
                res.setProname(list.get(i).getItemName());
                ArrayList<String> date = new ArrayList<>();
                ArrayList<BigDecimal> pro = new ArrayList<>();
                date.add(list.get(i).getUpdateTime().substring(0, 10));
                pro.add(list.get(i).getCurrentPrice());
                res.setDate(date);
                res.setProce(pro);
                map.put(list.get(i).getItemId(), res);
            } else {
                map.get(list.get(i).getItemId()).getDate().add(list.get(i).getUpdateTime().substring(0, 10));
                map.get(list.get(i).getItemId()).getProce().add(list.get(i).getCurrentPrice());
            }
        }
        Collection<StatisticsPurchaseByCustResp> values = map.values();
        List<StatisticsPurchaseByCustResp> arrayList = new ArrayList<StatisticsPurchaseByCustResp>(values);
        return arrayList;
    }

}
